<template>
  <el-dialog
    :title="!dataForm.roomId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户手机号" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="用户手机号"></el-input>
    </el-form-item>
    <el-form-item label="房屋类型" prop="roomType">
      <el-input v-model="dataForm.roomType" placeholder="房屋类型"></el-input>
    </el-form-item>
    <el-form-item label="小区id" prop="estateId">
      <el-input v-model="dataForm.estateId" placeholder="小区id"></el-input>
    </el-form-item>
    <el-form-item label="开盘时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="开盘时间"></el-input>
    </el-form-item>
    <el-form-item label="户型" prop="roomHtype">
      <el-input v-model="dataForm.roomHtype" placeholder="户型"></el-input>
    </el-form-item>
    <el-form-item label="参考价格" prop="roomPrice">
      <el-input v-model="dataForm.roomPrice" placeholder="参考价格"></el-input>
    </el-form-item>
    <el-form-item label="开发商" prop="roomDevelopers">
      <el-input v-model="dataForm.roomDevelopers" placeholder="开发商"></el-input>
    </el-form-item>
    <el-form-item label="产权年限" prop="roomAgelimit">
      <el-input v-model="dataForm.roomAgelimit" placeholder="产权年限"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          roomId: 0,
          userPhone: '',
          roomType: '',
          estateId: '',
          createTime: '',
          roomHtype: '',
          roomPrice: '',
          roomDevelopers: '',
          roomAgelimit: ''
        },
        dataRule: {
          userPhone: [
            { required: true, message: '用户手机号不能为空', trigger: 'blur' }
          ],
          roomType: [
            { required: true, message: '房屋类型不能为空', trigger: 'blur' }
          ],
          estateId: [
            { required: true, message: '小区id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '开盘时间不能为空', trigger: 'blur' }
          ],
          roomHtype: [
            { required: true, message: '户型不能为空', trigger: 'blur' }
          ],
          roomPrice: [
            { required: true, message: '参考价格不能为空', trigger: 'blur' }
          ],
          roomDevelopers: [
            { required: true, message: '开发商不能为空', trigger: 'blur' }
          ],
          roomAgelimit: [
            { required: true, message: '产权年限不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.roomId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.roomId) {
            this.$http({
              url: this.$http.adornUrl(`/user/housenewroom/info/${this.dataForm.roomId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userPhone = data.houseNewroom.userPhone
                this.dataForm.roomType = data.houseNewroom.roomType
                this.dataForm.estateId = data.houseNewroom.estateId
                this.dataForm.createTime = data.houseNewroom.createTime
                this.dataForm.roomHtype = data.houseNewroom.roomHtype
                this.dataForm.roomPrice = data.houseNewroom.roomPrice
                this.dataForm.roomDevelopers = data.houseNewroom.roomDevelopers
                this.dataForm.roomAgelimit = data.houseNewroom.roomAgelimit
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/housenewroom/${!this.dataForm.roomId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'roomId': this.dataForm.roomId || undefined,
                'userPhone': this.dataForm.userPhone,
                'roomType': this.dataForm.roomType,
                'estateId': this.dataForm.estateId,
                'createTime': this.dataForm.createTime,
                'roomHtype': this.dataForm.roomHtype,
                'roomPrice': this.dataForm.roomPrice,
                'roomDevelopers': this.dataForm.roomDevelopers,
                'roomAgelimit': this.dataForm.roomAgelimit
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
